package com.naizhuai.util;

import android.content.Context;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.TouchDelegate;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.AppContext;
import com.naizhuai.R;
import com.naizhuai.ui.components.GlideRoundTransform;

/**
 * @author：lichenxi
 * @date 2018/4/2 18
 * email：525603977@qq.com
 * Fighting
 */
public class ViewUtils {
    public static String text(TextView view){
        return view.getText().toString().trim();
    }

    /**
     * 优先展示上传的图片 默认展示人物名称图
     * @param imgurl 图片路径uri
     * @param name   人物名字
     * @param imageView 展示目标
     */
    public static void showAvatorOrName(String imgurl, String name, ImageView imageView){
        if (TextUtils.isEmpty(imgurl)){
            imageView.post(() -> imageView.setImageDrawable(DrawableUtils.setTextDrawable(name)));
        }else {
            showRoundImg(imgurl,imageView);
        }
    }

    public static void showImg(String imgurl,ImageView imageView){
        Glide.with(AppContext.app).load(imgurl)
                .placeholder(R.mipmap.wrong_image_icon)
                .error(R.mipmap.wrong_image_icon)
                .dontAnimate()
                .into(imageView);
    }

    public static void showRoundImg(String imgurl,ImageView imageView){
        ImageUtils.glideThumbViewByUrl(AppContext.app,imageView,imgurl);
    }

    /**
     * 扩大单个按钮的点击事件范围
     * @param view
     * @param expandTouchWidth
     */
    public static void setTouchDelegate(final View view, final int expandTouchWidth) {
        final View parentView = (View) view.getParent();
        parentView.post(new Runnable() {
            @Override
            public void run() {
                final Rect rect = new Rect();
                view.getHitRect(rect); // view构建完成后才能获取，所以放在post中执行
                // 4个方向增加矩形区域
                rect.top -= expandTouchWidth;
                rect.bottom += expandTouchWidth;
                rect.left -= expandTouchWidth;
                rect.right += expandTouchWidth;

                parentView.setTouchDelegate(new TouchDelegate(rect, view));
            }
        });
    }


}
